package se.webbzon.boltzmann.minigame.botany;

import java.awt.Rectangle;

import se.webbzon.boltzmann.configuration.Difficulty;
import se.webbzon.boltzmann.game.character.AbstractCharacter;
import se.webbzon.boltzmann.game.character.CharacterActionEnum;
import se.webbzon.boltzmann.minigame.chemistry.CarbonDioxideMolecule;
import se.webbzon.boltzmann.minigame.chemistry.FlourapatiteMolecule;
import se.webbzon.boltzmann.minigame.chemistry.HydrogenSulfideMolecule;
import se.webbzon.boltzmann.minigame.chemistry.NitriteMolecule;
import se.webbzon.boltzmann.minigame.chemistry.PhosphateMolecule;
import se.webbzon.boltzmann.minigame.chemistry.PhosphoricAcidMolecule;
import se.webbzon.boltzmann.minigame.chemistry.SandBoxMolecule;
import se.webbzon.boltzmann.minigame.chemistry.TriphenylPhosphateMolecule;
import se.webbzon.boltzmann.minigame.chemistry.WaterMolecule;
import se.webbzon.boltzmann.minigame.sandbox.DefaultSandBoxAnchor;
import se.webbzon.boltzmann.minigame.sandbox.DefaultSandBoxContainer;
import se.webbzon.boltzmann.minigame.sandbox.ReadOnlySandBox;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxAnchor;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxArrow;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxItem;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxPicture;
import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.resourceset.ResourceSet;
import se.webbzon.oschi01.textureset.Texture3D;

public class PhosphorusCycle extends Cycle {
	
	/*============================================================
	Protected Methods
	============================================================*/

	@Override protected void onCreate(ReadOnlySandBox box) {
		Rectangle r = new Rectangle();
		box.getBoundary(r);
		double w = r.getWidth(), h = r.getHeight();
		
		/** Create instances **/
		DefaultSandBoxContainer opoc6h53, po4, ca5op43f, h3po4;
		opoc6h53 = new DefaultSandBoxContainer(SandBoxMolecule.class);
		opoc6h53.setPosition(520, 184);
		po4 = new DefaultSandBoxContainer(SandBoxMolecule.class);
		po4.setPosition(272, 228);
		ca5op43f = new DefaultSandBoxContainer(SandBoxMolecule.class);
		ca5op43f.setPosition(64, 228);
		h3po4 = new DefaultSandBoxContainer(SandBoxMolecule.class);
		h3po4.setPosition(64, 132);

		SandBoxPicture plant, slug, death, water, earth, dirt;
		plant = new SandBoxPicture(Resources.getImageSet("biology_plant"),50);
		plant.setPosition(480, 20);
		plant.setSize(96, 96);
		plant.setSpeed(0.02);
		ResourceSet<Texture3D> textureSet = AbstractCharacter.getCharacterTextureSetMap().get("slug02").getLeftDown(CharacterActionEnum.WALK);
		slug = new SandBoxPicture(textureSet,50);
		slug.setPosition(560, 36);
		slug.setSize(64, 64);
		slug.setSpeed(0.2);
		death = new SandBoxPicture(Resources.getTextureSet("effect_skull"),50);
		death.setPosition(520, 116);
		death.setSize(32, 32);
		water = new SandBoxPicture(Resources.getTextureSet("terrain_water_water"),150);
		water.setSize(432, 128);
		water.setPosition(216, 136);
		water.setTransparency(0.5f);
		water.setSpeed(0.1);
		earth = new SandBoxPicture(Resources.getImageSet("biology_earth_2"),150);
		earth.setSize(192, 196);
		earth.setPosition(r.getWidth() - 96, 166);
		dirt = new SandBoxPicture(Resources.getTextureSet("terrain_dirt_dirt_01"),150);
		dirt.setSize(432, 64);
		dirt.setHRep(3);
		dirt.setPosition(216, 232);
		
		
		/** Create arrows and anchors **/
		SandBoxAnchor an1, an2, an3, an4, an5;
		an1 = new DefaultSandBoxAnchor(520, 228);
		an2 = new DefaultSandBoxAnchor(272, 132);
		an3 = new DefaultSandBoxAnchor(344, 184);
		an4 = new DefaultSandBoxAnchor(344, 116);
		an5 = new DefaultSandBoxAnchor(480, 116);
		
		SandBoxArrow[] arrows = {
				new SandBoxArrow(plant,death),
				new SandBoxArrow(slug,death),
				new SandBoxArrow(death,opoc6h53),
				new SandBoxArrow(opoc6h53,po4),
				new SandBoxArrow(po4,ca5op43f),
				new SandBoxArrow(ca5op43f,h3po4),
				new SandBoxArrow(po4,an2),
				new SandBoxArrow(opoc6h53,an3),
				new SandBoxArrow(an4,plant)
				/*new SandBoxArrow(so2,plant),
				new SandBoxArrow(so2,b2),
				new SandBoxArrow(mountain,h2s),
				new SandBoxArrow(mountain,so2),
				new SandBoxArrow(so2,h2so4),
				new SandBoxArrow(factory,so2)*/
		};
		arrows[3].addAnchor(an1);
		arrows[8].addAnchor(an5);
		
		/** Add items to cycle **/
		SandBoxItem[] items = {
				opoc6h53, po4, ca5op43f, h3po4,
				plant, slug, death, water, earth, dirt
		};
		
		for (SandBoxItem item : items)
			addItem(item);
		for (SandBoxItem item : arrows)
			addItem(item);
		
		/** Create and add molecules **/
		SandBoxMolecule[] molecules = {
				new WaterMolecule(), new HydrogenSulfideMolecule(),
				new PhosphateMolecule(), new PhosphoricAcidMolecule(),
				new TriphenylPhosphateMolecule(), new FlourapatiteMolecule(),
				new NitriteMolecule(), new CarbonDioxideMolecule()
		};
		
		for (SandBoxMolecule m : molecules) {
			m.setPosition(	0.1 + 0.8 * w * Math.random(), 
							0.1 + 0.8 * h * Math.random());
			m.setLifetime(-1);
			addItem(m);
		}
		
		/** Add solutions **/
		addSolution(opoc6h53,TriphenylPhosphateMolecule.class);
		addSolution(ca5op43f,FlourapatiteMolecule.class);
		addSolution(po4,PhosphateMolecule.class);
		addSolution(h3po4,PhosphoricAcidMolecule.class);
	}

	@Override protected void onDestroy(ReadOnlySandBox box) {
		clearSolution();
		clearItems();
	}
	
	@Override public Difficulty getDifficulty() {
		return Difficulty.HARD;
	}

}
